package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.chess.ChessPrefUtil;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SettingAdapter;
import flc.ast.bean.SettingBean;
import flc.ast.databinding.ActivityChessSettingBinding;
import flc.ast.dialog.SettingDialog;

public class ChessSettingActivity extends BaseAc<ActivityChessSettingBinding> {

    private Dialog mySettingDialog;
    private Dialog myStyleDialog;
    private List<SettingBean> mModestBeanList = new ArrayList<>();
    private List<SettingBean> mStyleBeanList = new ArrayList<>();
    private SettingAdapter settingAdapter;
    private SettingAdapter styleAdapter;
    private int modePos = 0, stylePos = 0;
    private boolean isComputerFirst;

    @Override
    protected int onCreate() {
        return R.layout.activity_chess_setting;
    }

    @Override
    protected void initView() {
        mDataBinding.ivChessSettingBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvModestResult.setOnClickListener(this);
        mDataBinding.ivWhoStart.setOnClickListener(this);
        mDataBinding.tvStyleResult.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvModestResult://让子设置
                mySettingDialog.show();
                break;
            case R.id.ivWhoStart://电脑先走
                isComputerFirst = !isComputerFirst;
                ChessPrefUtil.saveComputerFirst(isComputerFirst);
                mDataBinding.ivWhoStart.setImageResource(isComputerFirst ? R.drawable.kai1 : R.drawable.guan1);
                break;
            case R.id.tvStyleResult://棋子样式
                myStyleDialog.show();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == settingAdapter) {
            settingAdapter.getItem(modePos).setSelected(false);
            settingAdapter.getItem(position).setSelected(true);
            modePos = position;
            settingAdapter.notifyDataSetChanged();
            ChessPrefUtil.saveHandicap(modePos);
            mySettingDialog.dismiss();
            mDataBinding.tvModestResult.setText(settingAdapter.getItem(position).getSettingName());
        } else if (adapter == styleAdapter) {
            styleAdapter.getItem(stylePos).setSelected(false);
            styleAdapter.getItem(position).setSelected(true);
            stylePos = position;
            styleAdapter.notifyDataSetChanged();
            ChessPrefUtil.savePieceStyle(stylePos);
            myStyleDialog.dismiss();
            mDataBinding.tvStyleResult.setText(styleAdapter.getItem(position).getSettingName());
        }
    }

    @Override
    protected void initData() {
        getModesData();
        mDataBinding.tvModestResult.setText(mModestBeanList.get(ChessPrefUtil.getHandicap()).getSettingName());
        //让子设置对话框
        showSettingDialog();
        isComputerFirst = ChessPrefUtil.isComputerFirst();
        mDataBinding.ivWhoStart.setImageResource(isComputerFirst ? R.drawable.kai1 : R.drawable.guan1);
        getPieceData();
        mDataBinding.tvStyleResult.setText(mStyleBeanList.get(stylePos).getSettingName());
        //棋子样式对话框
        showStyleDialog();
    }

    private void getModesData() {
        mModestBeanList.add(new SettingBean(getString(R.string.setting_name1), false));
        mModestBeanList.add(new SettingBean(getString(R.string.setting_name2), false));
        mModestBeanList.add(new SettingBean(getString(R.string.setting_name3), false));
        mModestBeanList.add(new SettingBean(getString(R.string.setting_name4), false));
        modePos = ChessPrefUtil.getHandicap();
        mModestBeanList.get(modePos).setSelected(true);
    }

    private void getPieceData() {
        mStyleBeanList.add(new SettingBean(getString(R.string.style_name1), false));
        mStyleBeanList.add(new SettingBean(getString(R.string.style_name2), false));
        stylePos = ChessPrefUtil.getPieceStyle();
        mStyleBeanList.get(stylePos).setSelected(true);
    }

    //让子设置对话框
    private void showSettingDialog() {
        mySettingDialog = new Dialog(mContext, R.style.DialogStyle);
        //填充对话框的布局
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_setting, null);

        RecyclerView rvSetting = inflate.findViewById(R.id.rvDialogSettingList);

        rvSetting.setLayoutManager(new LinearLayoutManager(mContext));
        settingAdapter = new SettingAdapter();
        rvSetting.setAdapter(settingAdapter);
        settingAdapter.setList(mModestBeanList);
        settingAdapter.setOnItemClickListener(this);

        //将布局设置给Dialog
        mySettingDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = mySettingDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //获得窗体的属性
        DisplayMetrics dm = getResources().getDisplayMetrics();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
    }

    //棋子样式对话框
    private void showStyleDialog() {
        myStyleDialog = new Dialog(mContext, R.style.DialogStyle);
        //填充对话框的布局
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_style, null);

        RecyclerView rvSetting = inflate.findViewById(R.id.rvDialogStyleList);

        rvSetting.setLayoutManager(new LinearLayoutManager(mContext));
        styleAdapter = new SettingAdapter();
        rvSetting.setAdapter(styleAdapter);
        styleAdapter.setList(mStyleBeanList);
        styleAdapter.setOnItemClickListener(this);

        //将布局设置给Dialog
        myStyleDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = myStyleDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //获得窗体的属性
        DisplayMetrics dm = getResources().getDisplayMetrics();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
    }


}